import com.alibaba.fastjson.{JSON, JSONObject}
import org.apache.spark.sql.SparkSession
import org.elasticsearch.spark._

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2020/6/5 16:23
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object UpdateEsSalesAmount {
  def main(args: Array[String]): Unit = {

    val spark = SparkSession.builder()
      .appName(s"${this.getClass.getSimpleName}")
      .config("spark.debug.maxToStringFields", "2000")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.caseSensitive", "true")
      .config("es.nodes", "192.168.1.29")
      .config("es.port", "9200")
      .config("cluster.name","O2OElastic")
      .config("es.net.http.auth.user", "elastic")
      .config("es.net.http.auth.pass", "changeme")
      .master("local[*]")
      .getOrCreate()

    val sc = spark.sparkContext
    sc.setLogLevel("WARN")
    val index = "2021_wangyiyanxuan/wangyiyanxuan_2021_1"

    val data1 = sc.esJsonRDD(s"${index}",
      """
        |{
        |  "query": {
        |    "match_phrase": {
        |      "good_id": "3989051_300282112"
        |    }
        |  }
        |}
      """.stripMargin).values

    data1.map(line=>{
      val nObject: JSONObject = JSON.parseObject(line)
      val priceText: Double = nObject.getDouble("priceText")
      println("priceText:"+priceText)
//      val str = salesAmount.toDouble.formatted("%.2f")
      nObject.put("sellCount",2775)
      nObject.put("salesAmount",(priceText*2775).toDouble)
      nObject
    }).saveToEs(s"${index}",
        Map("es.mapping.id" -> "good_id", "es.nodes" -> s"192.168.1.29",
          "es.port" -> "9200",
          "cluster.name" -> "Es-OTO-Data"))

  sc.stop()
}
}
